// pages/active/interaction/index.js
import Toast from 'tdesign-miniprogram/toast/index';
// 接口
import { fetchActivitCommonlist, fetchActivitAddcommon } from '~/request/api';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    row: {},
    list: [],
    hasLoaded: false,
    loadMoreStatus: 0,
    comment: '',
    activity_info: null,
  },

  // 分页变量
  total: 0,
  page: 1,
  page_size: 20,

  change(e) {
    let comment = e.detail.value;
    this.setData({ comment });
  },

  // 点击发言
  handleComment() {
    const { comment } = this.data;
    this.activitAddcommon(comment);
  },

  /**
   * 接口函数
   */

  // 获取留言列表
  async activitCommonlist(params) {
    const { isPage, ..._params } = params;
    const { page, page_size } = this;
    let { list, loadMoreStatus, row } = this.data;
    const { hd_id, term_id } = row;
    const _page = isPage ? page + 1 : page;
    let query = { page: _page, page_size, hd_id, term_id, ..._params };
    if (loadMoreStatus !== 0) return;
    this.setData({
      loadMoreStatus: 1,
    });
    try {
      const res = await fetchActivitCommonlist(query);
      const { data, total, activity_info } = res;
      if (total === 0) {
        this.total = total;
        this.setData({
          hasLoaded: true,
          loadMoreStatus: 0,
          list: [],
          activity_info,
        });
        return;
      }
      if (query.page == 1) list = [];
      const _list = list.concat(data);
      const _loadMoreStatus = list.length === total ? 2 : 0;
      this.page = query?.page || 1;
      this.total = total;
      this.setData({
        list: _list,
        loadMoreStatus: _loadMoreStatus,
        activity_info
      });
    } catch (error) {
      console.log(error);
    }
    this.setData({
      hasLoaded: true,
    });
  },

  // 添加留言
  async activitAddcommon(comment) {
    const { hd_id, term_id } = this.data.row;
    const values = { hd_id, term_id, comment };
    if (!comment) {
      wx.showModal({
        content: '请输入留言信息！',
        showCancel: false,
      })
      return;
    }
    try {
      const res = await fetchActivitAddcommon({ ...values });
      Toast({
        context: this,
        selector: '#t-toast',
        message: res.msg,
      });
      // 重置分页状态
      this.setData({ loadMoreStatus: 0 })
      // 查询分页数据
      this.activitCommonlist({ page: 1, isPage: false });
    } catch (error) {
      console.log(error);
    }
    // 重置
    this.setData({ comment: '' })
    wx.pageScrollTo({
      scrollTop: 0
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      row: options,
    });
    this.activitCommonlist({ ...options, isPage: false });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const { list } = this.data;
    const { total = 0 } = this;
    if (list.length === total) {
      this.setData({
        loadMoreStatus: 2,
      });
      return;
    }
    this.activitCommonlist({ isPage: true });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
